//
//  XMWaitPayOrderView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/18.
//

#import "XMWaitPayOrderView.h"
#import "XMConfirmOrderMeetCell.h"

@interface XMWaitPayOrderView ()

@property (nonatomic, strong) UILabel *countDownLabel;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) XMConfirmOrderMeetCell *meetInfoView;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *payTotalPriceLabel;
@property (nonatomic, strong) UILabel *orderNoLabel;
@property (nonatomic, strong) UILabel *dateLabel;

@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger timeInter;

@property (nonatomic, strong) XMOrderDetailModel *orderDetailModel;

@end

@implementation XMWaitPayOrderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"待付款";
    self.navView.backgroundColor = kColorHEX(0xF6F6F6);
    self.backgroundColor = kColorHEX(0xF6F6F6);
    
    [self addSubview:self.countDownLabel];
    
    UIImageView *addressBgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kNavagationBarH + KScaleWidth(40.0), self.width, KScaleWidth(76.0))];
    addressBgImgView.image = ImageNamed(@"order_detail_address_bg_img");
    [self addSubview:addressBgImgView];
    
    [addressBgImgView addSubview:self.addressLabel];
    [addressBgImgView addSubview:self.phoneLabel];
    
    UIView *meetInfoBgView = [[UIView alloc] initWithFrame:CGRectMake(0, addressBgImgView.maxY + KScaleWidth(8.0), self.width, KScaleWidth(270.0))];
    meetInfoBgView.backgroundColor = kColorHEX(0xffffff);
    [self addSubview:meetInfoBgView];
    
    [meetInfoBgView addSubview:self.avatarImgView];
    [meetInfoBgView addSubview:self.nameLabel];
    UIView *meetBgView = [[UIView alloc] initWithFrame:CGRectMake(0, KScaleWidth(58.0), self.width, KScaleWidth(100.0))];
    [meetInfoBgView addSubview:meetBgView];
    [meetBgView addSubview:self.meetInfoView];
    UILabel *priceTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), meetBgView.maxY + KScaleWidth(20.0), self.width/2 - KScaleWidth(16.0), KScaleWidth(25.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
    priceTitleLabel.text = @"产品总价";
    [meetInfoBgView addSubview:priceTitleLabel];
    [meetInfoBgView addSubview:self.priceLabel];
    [meetInfoBgView addSubview:self.payTotalPriceLabel];
    
    UIView *orderInfoBgView = [[UIView alloc] initWithFrame:CGRectMake(0, meetInfoBgView.maxY + KScaleWidth(8.0), self.width, KScaleWidth(84.0))];
    orderInfoBgView.backgroundColor = kColorHEX(0xffffff);
    [self addSubview:orderInfoBgView];
    {
        UILabel *orderTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(12.0), self.width/2 - KScaleWidth(16.0), KScaleWidth(30.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
        orderTitleLabel.text = @"订单编号";
        [orderInfoBgView addSubview:orderTitleLabel];
        UIButton *copyBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(self.width - KScaleWidth(16.0) - KScaleWidth(54.0), orderTitleLabel.y + (orderTitleLabel.height - KScaleWidth(24.0))/2, KScaleWidth(54.0), KScaleWidth(24.0)) title:@"复制" titleColor:kColorHEX(0x666666)];
        [copyBtn setRadius:copyBtn.height/2 borderColor:kColorHEX(0xCCCCCC) borderWidth:1];
        [copyBtn addTarget:self action:@selector(copyAction:) forControlEvents:UIControlEventTouchUpInside];
        [orderInfoBgView addSubview:copyBtn];
        [orderInfoBgView addSubview:self.orderNoLabel];
        
        UILabel *dateTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), orderTitleLabel.maxY, self.width/2 - KScaleWidth(16.0), orderTitleLabel.height) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
        dateTitleLabel.text = @"下单时间";
        [orderInfoBgView addSubview:dateTitleLabel];
        [orderInfoBgView addSubview:self.dateLabel];
    }
    UIButton *cancelOrderBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(self.width - KScaleWidth(210.0) - KScaleWidth(90.0), orderInfoBgView.maxY + KScaleWidth(50.0), KScaleWidth(90.0), KScaleWidth(38.0)) title:@"取消订单" titleColor:kColorHEX(0x333333)];
    [cancelOrderBtn setRadius:cancelOrderBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
    [cancelOrderBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelOrderBtn];
    
    UIButton *contactBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(self.width - KScaleWidth(112.0) - KScaleWidth(90.0), cancelOrderBtn.y, KScaleWidth(90.0), KScaleWidth(38.0)) title:@"联系对方" titleColor:kColorHEX(0x333333)];
    [contactBtn setRadius:contactBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
    [contactBtn addTarget:self action:@selector(contactAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:contactBtn];
    
    UIButton *payBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(14.0) frame:CGRectMake(self.width - KScaleWidth(16.0) - KScaleWidth(90.0), contactBtn.y, KScaleWidth(90.0), KScaleWidth(38.0)) title:@"立即付款" titleColor:kColorHEX(0x333333)];
    [payBtn setGradientLayerWithLocations:@[@(0), @(1)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
    [payBtn setCornerRadius:payBtn.height/2];
    [payBtn addTarget:self action:@selector(payAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:payBtn];
}

- (void)setContentWithOrderDetailModel:(XMOrderDetailModel *)orderDetailModel {
    _orderDetailModel = orderDetailModel;
    _addressLabel.text = [NSString stringWithFormat:@"见面位置：%@", orderDetailModel.address];
    _phoneLabel.text = [NSString stringWithFormat:@"%@  %@", orderDetailModel.nickname, orderDetailModel.phone];
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:orderDetailModel.avatar] placeholderImage:ImageNamed(@"mine_avatar_icon")];
    _nameLabel.text = orderDetailModel.nickname;
    [_meetInfoView setContentWithOrderDetailModel:orderDetailModel];
    _priceLabel.text = [NSString stringWithFormat:@"¥%@", orderDetailModel.yinfuPrice];
    _orderNoLabel.text = orderDetailModel.orderNo;
    _dateLabel.text = [[NSDate dateWithTimeIntervalSince1970:[orderDetailModel.orderTime integerValue]/1000] dateStringWithFormat:@"yyyy-MM-dd HH:mm"];
    NSString *payTotalPriceStr = [NSString stringWithFormat:@"待付款 ¥%@", orderDetailModel.price];
    NSMutableAttributedString *payTotalPriceAttr = [[NSMutableAttributedString alloc] initWithString:payTotalPriceStr attributes:@{NSFontAttributeName : KBoldFontSize(18.0), NSForegroundColorAttributeName : kColorHEX(0xDD4B39)}];
    [payTotalPriceAttr addAttributes:@{NSFontAttributeName : KFontSize(16.0), NSForegroundColorAttributeName : kColorHEX(0x333333)} range:NSMakeRange(0, 3)];
    [_payTotalPriceLabel setAttributedText:payTotalPriceAttr];
    orderDetailModel.countdownSeconds = @"1234";
    if ([orderDetailModel.countdownSeconds integerValue] > 0) {
        if (_timer) {
            [_timer invalidate];
        }
        _timeInter = [orderDetailModel.countdownSeconds integerValue];
        [self setCountDownTime];
        XMWeakSelf;
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
            weakSelf.timeInter -= 1;
            [weakSelf setCountDownTime];
        }];
    }
    else {
        [_countDownLabel setAttributedText:[[NSMutableAttributedString alloc] initWithString:@""]];
        _countDownLabel.text = @"超时支付自动取消订单";
    }
}

- (void)copyAction:(UIButton *)button {
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    pab.string = _orderNoLabel.text;
    if ([pab.string isEqualToString:_orderNoLabel.text]) {
        [MBProgressHUD showSuccess:@"已复制到剪切板"];
    }
}

- (void)setCountDownTime {
    if (_timeInter <= 0) {
        [_timer invalidate];
        [_countDownLabel setAttributedText:[[NSMutableAttributedString alloc] initWithString:@""]];
        _countDownLabel.text = @"超时支付自动取消订单";
        return;
    }
    NSString *timeStr = @"";
    if (_timeInter > 60) {
        timeStr = [NSString stringWithFormat:@"剩余%ld:%ld支付关闭", _timeInter/60, _timeInter%60];
    }
    else {
        timeStr = [NSString stringWithFormat:@"剩余%ld支付关闭", _timeInter];
    }
    NSMutableAttributedString *timeAttr = [[NSMutableAttributedString alloc] initWithString:timeStr attributes:@{NSFontAttributeName : KFontSize(14.0), NSForegroundColorAttributeName : kColorHEX(0x666666)}];
    [timeAttr addAttributes:@{NSForegroundColorAttributeName : kColorHEX(0xDD4B39)} range:NSMakeRange(2, timeStr.length - 6)];
    [_countDownLabel setAttributedText:timeAttr];
}

#pragma mark - Init
- (void)cancelAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(cancelOrderWithOrderId:)]) {
        [self.delegate cancelOrderWithOrderId:_orderDetailModel.orderId];
    }
}

- (void)contactAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(contactWithPhone:)]) {
        [self.delegate contactWithPhone:self.orderDetailModel.phone];
    }
}

//立即支付
- (void)payAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(payActionWithOrderId:price:)]) {
        [self.delegate payActionWithOrderId:self.orderDetailModel.orderId
                                      price:self.orderDetailModel.price];
    }
}

#pragma mark - Init

- (UILabel *)countDownLabel {
    if (!_countDownLabel) {
        _countDownLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(26.0), kNavagationBarH, self.width - KScaleWidth(26.0), KScaleWidth(28.0)) font:KFontSize(14.0) textColor:kColorHEX(0x666666)];
    }
    return _countDownLabel;
}

- (UILabel *)addressLabel {
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, KScaleWidth(10.0), self.width - KScaleWidth(30.0), KScaleWidth(28.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333) textAlignment:NSTextAlignmentRight];
    }
    return _addressLabel;
}

- (UILabel *)phoneLabel {
    if (!_phoneLabel) {
        _phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.addressLabel.maxY, self.width - KScaleWidth(30.0), KScaleWidth(26.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333) textAlignment:NSTextAlignmentRight];
    }
    return _phoneLabel;
}

- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(16.0), KScaleWidth(36.0), KScaleWidth(36.0))];
        [_avatarImgView setCornerRadius:_avatarImgView.height/2];
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImgView.maxX + KScaleWidth(8.0), _avatarImgView.y, self.width - _avatarImgView.maxX - KScaleWidth(8.0) - KScaleWidth(16.0), _avatarImgView.height) font:KFontSize(18.0) textColor:kColorHEX(0x333333)];
    }
    return _nameLabel;
}

- (XMConfirmOrderMeetCell *)meetInfoView {
    if (!_meetInfoView) {
        _meetInfoView = [[XMConfirmOrderMeetCell alloc] initWithFrame:CGRectMake(0, KScaleWidth(58.0), self.width, KScaleWidth(100.0))];
    }
    return _meetInfoView;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width/2, self.meetInfoView.superview.maxY + KScaleWidth(10.0), self.width/2 - KScaleWidth(16.0), KScaleWidth(30.0)) font:KBoldFontSize(18.0) textColor:kColorHEX(0x333333) textAlignment:NSTextAlignmentRight];
    }
    return _priceLabel;
}

- (UILabel *)payTotalPriceLabel {
    if (!_payTotalPriceLabel) {
        _payTotalPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), self.priceLabel.maxY + KScaleWidth(25.0), self.width - KScaleWidth(16.0)*2, KScaleWidth(32.0))];
        _payTotalPriceLabel.textAlignment = NSTextAlignmentRight;
    }
    return _payTotalPriceLabel;
}

- (UILabel *)orderNoLabel {
    if (!_orderNoLabel) {
        _orderNoLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(100.0), KScaleWidth(12.0), self.width - KScaleWidth(100.0) - KScaleWidth(82.0), KScaleWidth(30.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666) textAlignment:NSTextAlignmentRight];
    }
    return _orderNoLabel;
}

- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(100.0), self.orderNoLabel.maxY, self.width - KScaleWidth(100.0) - KScaleWidth(16.0), _orderNoLabel.height) font:KFontSize(15.0) textColor:kColorHEX(0x666666) textAlignment:NSTextAlignmentRight];
    }
    return _dateLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
